####### REPLICATION CODE FOR Proctor, Cypher, Ackeley, and Warner-Evans 2025
####### Published in PS ##########
###### Please refer to code book for additional information about the data and codes

library("knitr")
library("magrittr")
library("dplyr")
library("ggplot2")
library("tidyr")
library(foreign)
library(haven)

####### CODE TO CREATE PROPORTIONS REPORTED IN TABLE 1 #######
####### Read in 2016-2022 CES
# read in 2016 - 2022 data
cces.2016 <- read_dta("CES2016.dta")
load("CES2017.RData")
cces.2017 <- table
load("cces18.RData")
cces.2018 <- x
load("CCES19.RData")
cces.2019 <- table
cces.2020 <- read.csv("CES20.csv", header = T)
load("CES21.RData")
cces.2021 <- table
cces.2022 <- read.csv("CES22_Common.csv", header = T)

######## CODE LGBTQ+ Identifier for each survey wave #######
## 2016 
table(cces.2016$sexuality)
table(cces.2016$trans)
cces.2016$lgbt <- ifelse(c(cces.2016$sexuality >= 2 & cces.2016$sexuality <= 4) |
                           cces.2016$trans == 1, 1, 0)
table(cces.2016$lgbt)
# repeat for 2017
table(cces.2017$sexuality)
table(cces.2017$trans)
cces.2017$lgbt <- ifelse(c(cces.2017$sexuality >= 2 & cces.2017$sexuality <= 4) |
                           cces.2017$trans == 1, 1, 0)
table(cces.2017$lgbt)
# repeat for sexuality variable in 2018, 2019, 2020, 2021, and 2022 datasets
table(cces.2018$sexuality)
table(cces.2018$trans)
cces.2018$lgbt <- ifelse(c(cces.2018$sexuality >= 2 & cces.2018$sexuality <= 4) |
                           cces.2018$trans == 1, 1, 0)
table(cces.2018$lgbt)

table(cces.2019$sexuality)
table(cces.2019$trans)
cces.2019$lgbt <- ifelse(c(cces.2019$sexuality >= 2 & cces.2019$sexuality <= 4) |
                           cces.2019$trans == 1, 1, 0)
table(cces.2019$lgbt)

table(cces.2020$sexuality)
table(cces.2020$trans)
cces.2020$lgbt <- ifelse(c(cces.2020$sexuality >= 2 & cces.2020$sexuality <= 4) |
                           cces.2020$trans == 1, 1, 0)
table(cces.2020$lgbt)

table(cces.2021$sexuality)
table(cces.2021$transgender)
cces.2021$lgbt <- ifelse(c(cces.2021$sexuality >= 2 & cces.2021$sexuality <= 4) |
                           cces.2021$transgender == 1, 1, 0)

table(cces.2021$lgbt)

table(cces.2022$sexuality)
table(cces.2022$transgender)
cces.2022$lgbt <- ifelse(c(cces.2022$sexuality >= 2 & cces.2022$sexuality <= 4) |
                           cces.2022$transgender == 1, 1, 0)
table(cces.2022$lgbt)

### create table with one column that contains proportions for LGBT == 0
### and another column that contains proportions for LGBT == 1
# create a vector of proportions for LGBT == 0
lgbt.prop <- cbind(prop.table(table(cces.2016$lgbt)), prop.table(table(cces.2017$lgbt)), 
                   prop.table(table(cces.2018$lgbt)), prop.table(table(cces.2019$lgbt)), 
                   prop.table(table(cces.2020$lgbt)), prop.table(table(cces.2021$lgbt)), 
                   prop.table(table(cces.2022$lgbt)))
lgbt.prop

### examine and clean union variables for all years of CES data
table(cces.2016$union)
table(cces.2017$union)
table(cces.2018$union)
table(cces.2019$union)
table(cces.2020$union)
table(cces.2021$union)
table(cces.2022$union)

# recode union variable to create a binary variable for union membership
cces.2016$union <- ifelse(cces.2016$union == 1, 1, 0)
cces.2017$union <- ifelse(cces.2017$union == 1, 1, 0)
cces.2018$union <- ifelse(cces.2018$union == 1, 1, 0)
cces.2019$union <- ifelse(cces.2019$union == 1, 1, 0)
cces.2020$union <- ifelse(cces.2020$union == 1, 1, 0)
cces.2021$union <- ifelse(cces.2021$union == 1, 1, 0)
cces.2022$union <- ifelse(cces.2022$union == 1, 1, 0)


#### Create table that give proportion of union membership for LGBTQ/Non-LGBTQ
a <-rbind(prop.table(table(cces.2016$union[cces.2016$lgbt == 1])),
          prop.table(table(cces.2017$union[cces.2017$lgbt == 1 ])),
          prop.table(table(cces.2018$union[cces.2018$lgbt == 1])),
          prop.table(table(cces.2019$union[cces.2019$lgbt == 1])),
          prop.table(table(cces.2020$union[cces.2020$lgbt == 1])),
          prop.table(table(cces.2021$union[cces.2021$lgbt == 1])),
          prop.table(table(cces.2022$union[cces.2022$lgbt == 1])))
b <-rbind(prop.table(table(cces.2016$union[cces.2016$lgbt == 0])),
          prop.table(table(cces.2017$union[cces.2017$lgbt == 0 ])),
          prop.table(table(cces.2018$union[cces.2018$lgbt == 0])),
          prop.table(table(cces.2019$union[cces.2019$lgbt == 0])),
          prop.table(table(cces.2020$union[cces.2020$lgbt == 0])),
          prop.table(table(cces.2021$union[cces.2021$lgbt == 0])),
          prop.table(table(cces.2022$union[cces.2022$lgbt == 0])))
table_1 <- cbind(a[,2], b[,2], a[,2] - b[,2]) * 100


############################################################################################################
### REPLICATION CODE FOR FIGURES 1-3
unionData <- read.csv("cleaned_all_tags.csv")
table(unionData$tag, unionData$organization)

unionData <- unionData[!(unionData$tag %in% c("ACT", "ISN","ACTsy","ACTsy.CEL","ACTsy.P","ACTsu","ACTsu.MO","ACTsu.COM","PO","EDU","EM","EN","INV","TOP", "NON")), ]
# Remove categories rather than specific tags:
unionData <- unionData[!(unionData$tag %in% c("ACT", "ISN","ACTsy","ACTsy.CEL","ACTsy.P","ACTsu","ACTsu.MO","ACTsu.COM","PO","EDU","EM","EN","INV","TOP", "NON")), ]

# Split up by symbolic and substantive representation and agenda setting

syTags <- unionData[unionData$tag %in% c("ACTsy.SOL", "ACTsy.CEL","ACTsy.CEL.PR","ACTsy.CEL.HI","ACTsy.P","ACTsy.P.S","ACTsy.P.C","ACTsy.P.O","ACTsy.ACK"), ]
suTags <- unionData[unionData$tag %in% c("ACTsu.EDUN","ACTsu.EDUW","ACTsu.MO","ACTsu.MO.B","ACTsu.MO.M","ACTsu.MO.CA","ACTsu.COM","ACTsu.COM.E","ACTsu.COM.G","ACTsu.BAR","ACTsu.EMV","ACTsu.PV"), ]
issueTags <- unionData[!unionData$tag %in% c("ACTsy", "ACTsy.SOL", "ACTsy.CEL","ACTsy.CEL.PR","ACTsy.CEL.HI","ACTsy.P","ACTsy.P.S","ACTsy.P.C","ACTsy.P.O","ACTsy.ACK","ACTsu","ACTsu.EDUN","ACTsu.EDUW","ACTsu.MO","ACTsu.MO.B","ACTsu.MO.M","ACTsu.MO.CA","ACTsu.COM","ACTsu.COM.E","ACTsu.COM.G","ACTsu.BAR","ACTsu.EMV","ACTsu.PV"), ]

# Clean up names of tags
suTags$tag[suTags$tag == "ACTsu.BAR"] <- "Collective Bargaining"
suTags$tag[suTags$tag == "ACTsu.COM.E"] <- "Community Events"
suTags$tag[suTags$tag == "ACTsu.EDUN"] <- "Educate Non-LGBTQ+"
suTags$tag[suTags$tag == "ACTsu.EDUW"] <- "Educate LGBTQ+"
suTags$tag[suTags$tag == "ACTsu.EMV"] <- "LGBTQ+ Leadership"
suTags$tag[suTags$tag == "ACTsu.MO.B"] <- "Boycotts"
suTags$tag[suTags$tag == "ACTsu.MO.CA"] <- "Call-in-Campaigns"
suTags$tag[suTags$tag == "ACTsu.MO.M"] <- "Marches"
suTags$tag[suTags$tag == "ACTsu.PV"] <- "Policy-Writing"
syTags$tag[syTags$tag == "ACTsy.ACK"] <- "Acknowledge\nIntersection"
syTags$tag[syTags$tag == "ACTsy.CEL.HI"] <- "Celebrate History"
syTags$tag[syTags$tag == "ACTsy.CEL.PR"] <- "Celebrate Individual"
syTags$tag[syTags$tag == "ACTsy.P.C"] <- "Pressure"
syTags$tag[syTags$tag == "ACTsy.P.O"] <- "Oppose Policy"
syTags$tag[syTags$tag == "ACTsy.P.S"] <- "Support Policy"
syTags$tag[syTags$tag == "ACTsy.SOL"] <- "Stand in Solidarity"


# Calculate rates of symbolic representation 
group1 <- c("AFL")
group2 <- c("P@W")

# Filter the data into the two groups
group1sy <- filter(syTags, organization %in% group1)
group2sy <- filter(syTags, organization %in% group2)

totalGroup1sy <- nrow(group1sy) ### 22 AFL-CIO
totalGroup2sy <- nrow(group2sy) ### 225 PAW

# Aggregate the data by tag type and group for group 1
tag_counts_group1sy <- group1sy %>%
  group_by(tag) %>%
  summarise(Count = n()) %>%
  mutate(Percentage = (Count / totalGroup1sy) * 100)

# Aggregate the data by tag type and group for group 2
tag_counts_group2sy <- group2sy %>%
  group_by(tag) %>%
  summarise(Count = n()) %>%
  mutate(Percentage = (Count / totalGroup2sy) * 100)

# Combine the two data frames
combined_tag_counts_percentages_sy <- rbind(
  mutate(tag_counts_group1sy, Group = "AFL-CIO"),
  mutate(tag_counts_group2sy, Group = "PAW")
)

custom_palette <- c("black", "grey")

# Plot the data using ggplot2
a <- ggplot(combined_tag_counts_percentages_sy, aes(x = tag, y = Percentage, fill = Group)) +
  geom_bar(stat = "identity", position = position_dodge(width = 0.9), width = 0.6) +
  geom_text(aes(label = ifelse(Count > 0, paste("n =", Count), "")), 
            position = position_dodge(width = 0.9), 
            vjust = -0.5,  # Adjust vertical position
            size = 3.5) +
  geom_hline(yintercept = 0, color = "black", size = 0.5) +  # Add axis line at y = 0
  labs(title = "Figure 1: Symbolic representation between AFL-CIO and PAW",
       x = "",
       y = "Percentage") +
  scale_fill_manual(values = custom_palette) +  # Applying custom color palette
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 14),
        legend.position = "bottom",
        text = element_text(color = "black"),
        panel.background = element_rect(fill = "white"),  # Changing background to white
        plot.background = element_rect(fill = "white"),  # Changing background to white
        legend.background = element_rect(fill = "white"),  # Changing background to white
        panel.grid.major = element_blank(),  # Remove major grid lines
        panel.grid.minor = element_blank(),  # Remove minor grid lines
        strip.background = element_rect(fill = "white"),
        strip.text = element_text(color = "black"))
a
### Use this  code to save the plot as a tiff file
ggsave("figure1.tiff", 
       plot = a, width = 10, height = 6, units = "in", dpi = 300)

############################################ Compare PAW to AFL substantive work
# Filter the data into the two groups
group1su <- filter(suTags, organization %in% group1)
group2su <- filter(suTags, organization %in% group2)

totalGroup1su <- nrow(group1su) # 4
totalGroup2su <- nrow(group2su) # 87

# Aggregate the data by tag type and group for group 1
tag_counts_group1su <- group1su %>%
  group_by(tag) %>%
  summarise(Count = n()) %>%
  mutate(Percentage = (Count / totalGroup1su) * 100) %>%
  add_row(tag = "Boycotts", Count = 0, Percentage = 0) %>%
  add_row(tag = "Call-in-Campaigns", Count = 0, Percentage = 0) %>%
  add_row(tag = "Collective Bargaining", Count = 0, Percentage = 0) %>%
  add_row(tag = "Community Events", Count = 0, Percentage = 0) %>%
  add_row(tag = "Educate LGBTQ+", Count = 0, Percentage = 0) %>%
  add_row(tag = "Educate Non-LGBTQ+", Count = 0, Percentage = 0) %>%
  add_row(tag = "LGBTQ+ Leadership", Count = 0, Percentage = 0) %>%
  add_row(tag = "Marches", Count = 0, Percentage = 0) %>%
  add_row(tag = "Policy-Writing", Count = 0, Percentage = 0)


# Aggregate the data by tag type and group for group 2
tag_counts_group2su <- group2su %>%
  group_by(tag) %>%
  summarise(Count = n()) %>%
  mutate(Percentage = (Count / totalGroup2su) * 100)

# Combine the two data frames
combined_tag_counts_percentages_su <- rbind(
  mutate(tag_counts_group1su, Group = "AFL-CIO"),
  mutate(tag_counts_group2su, Group = "PAW")
)

# Plot the data using ggplot2
b <- ggplot(combined_tag_counts_percentages_su, aes(x = tag, y = Percentage, fill = Group)) +
  geom_bar(stat = "identity", position = position_dodge(width = 0.9), width = 0.6) + 
  geom_text(aes(label = ifelse(Count > 0, paste("N =", Count), "")), 
            position = position_dodge(width = 1), 
            vjust = -0.4,  # Adjust vertical position
            size = 3.5) +
  geom_hline(yintercept = 0, color = "black", size = 0.5) +  # Add axis line at y = 0
  labs(title = "Figure 2: Substantive representation between AFL-CIO and PAW",
       x = "",
       y = "Percentage") +
  scale_fill_manual(values = custom_palette) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 14),
        legend.position = "bottom",
        text = element_text(color = "black"),
        panel.background = element_rect(fill = "white"),  # Changing background to white
        plot.background = element_rect(fill = "white"),  # Changing background to white
        legend.background = element_rect(fill = "white"),  # Changing background to white
        panel.grid.major = element_blank(),  # Remove major grid lines
        panel.grid.minor = element_blank(),  # Remove minor grid lines
        strip.background = element_rect(fill = "white"),
        strip.text = element_text(color = "black"))
b
# save file
ggsave("figure2.tiff", 
       plot = b, width = 10, height = 6, units = "in", dpi = 300)

############### Calculating percentage/frequencies for substantive and symbolic rep
###### These calculations align with frequencies reported in Figures 1 and 2.
### AFL-CIO (n = 22), symbolic
### AFL-CIO (n = 4), substantive
#### 85% symbolic, 15% substantive
### PAW (n = 225), symbolic
### PAW (n = 87), substantive
#### 72% symbolic, 28% substantive




###############################################################
##############################################################
# Compare PAW to AFL Issue counts
# clean issue tags
issueTags$tag[issueTags$tag == "EDU.EDUM"] <- "Educational Materials"
issueTags$tag[issueTags$tag == "EDU.TR"] <- "Educational Training"
issueTags$tag[issueTags$tag == "EM.LEAD"] <- "Empower Leadership"
issueTags$tag[issueTags$tag == "EM.PSD"] <- "Professional Development"
issueTags$tag[issueTags$tag == "EM.WOR"] <- "Workers' Rights"
issueTags$tag[issueTags$tag == "EN.AD"] <- "Anti-Discrimination"
issueTags$tag[issueTags$tag == "EN.CB"] <- "Community building"
issueTags$tag[issueTags$tag == "EN.RE"] <- "Respect/Acceptance"
issueTags$tag[issueTags$tag == "EN.SAF"] <- "Safety"
issueTags$tag[issueTags$tag == "EN.VIS"] <- "LGBTQ+ Visibility"
issueTags$tag[issueTags$tag == "INV.PI"] <- "LGBTQ+ Political Involvement"
issueTags$tag[issueTags$tag == "INV.UM"] <- "LGBTQ+ Union Membership"
issueTags$tag[issueTags$tag == "INV.VO"] <- "Voting"
issueTags$tag[issueTags$tag == "INV.PI"] <- "LGBTQ+ Political Involvement"
issueTags$tag[issueTags$tag == "PO.IN"] <- "Individual Policy"
issueTags$tag[issueTags$tag == "PO.ST"] <- "Structural Policy"
issueTags$tag[issueTags$tag == "TOP.CL"] <- "Climate Change"
issueTags$tag[issueTags$tag == "TOP.D"] <- "Democracy"
issueTags$tag[issueTags$tag == "TOP.DB"] <- "Domestic Partner Benefits"
issueTags$tag[issueTags$tag == "TOP.H"] <- "Healthcare"
issueTags$tag[issueTags$tag == "TOP.HT"] <- "Human Trafficking"
issueTags$tag[issueTags$tag == "TOP.IS"] <- "Immigration"
issueTags$tag[issueTags$tag == "TOP.ME"] <- "Marriage Equality"
issueTags$tag[issueTags$tag == "TOP.MH"] <- "Mental Health"
issueTags$tag[issueTags$tag == "TOP.PB"] <- "Police Brutality"
issueTags$tag[issueTags$tag == "TOP.RAC"] <- "Racism"
issueTags$tag[issueTags$tag == "TOP.RG"] <- "Religion"
issueTags$tag[issueTags$tag == "TOP.RJ"] <- "Reproductive Justice"
issueTags$tag[issueTags$tag == "TOP.TG"] <- "Trans Issues"
issueTags$tag[issueTags$tag == "TOP.TPP"] <- "Trans-Pacific Partnership"
# Filter the data into the two groups
group1Issues<- filter(issueTags, organization %in% group1)
group2Issues <- filter(issueTags, organization %in% group2)

# Aggregate the data by tag type and group for group 1
group1IssueCounts <- group1Issues %>%
  group_by(tag) %>%
  summarise(Count = n()) 

# Aggregate the data by tag type and group for group 2
group2IssueCounts <- group2Issues %>%
  group_by(tag) %>%
  summarise(Count = n())

# Combine the two data frames
combinedTagCountsIssue <- rbind(
  mutate(group1IssueCounts, Group = "AFL-CIO"),
  mutate(group2IssueCounts, Group = "P@W")
)



# But we should do this by percent of total tags

totalGroup1Is <- nrow(group1Issues)
totalGroup2Is <- nrow(group2Issues)


# Aggregate the data by tag type and group for group 1
tag_counts_group1_Is <- group1Issues %>%
  group_by(tag) %>%
  summarise(Count = n()) %>%
  mutate(Percentage = (Count / totalGroup1Is) * 100) %>%
  add_row(tag = "Immigration", Count = 0, Percentage = 0) %>%
  add_row(tag = "Racism", Count = 0, Percentage = 0) 

# Aggregate the data by tag type and group for group 2
tag_counts_group2_Is <- group2Issues %>%
  group_by(tag) %>%
  summarise(Count = n()) %>%
  mutate(Percentage = (Count / totalGroup2Is) * 100) %>%
  add_row(tag = "LGBTQ+ Visibility", Count = 0, Percentage = 0)

# Combine the two data frames
combined_tag_counts_percentages_Is <- rbind(
  mutate(tag_counts_group1_Is, Group = "AFL-CIO"),
  mutate(tag_counts_group2_Is, Group = "PAW")
) %>% print(n = 45)

tags_to_remove <- c("Climate Change", "Democracy","Community building","Educational Materials","Educational Training","Empower Leadership","Human Trafficking","Individual Policy","LGBTQ+ Political Involvement","Mental Health","Professional Development","Respect/Acceptance","UN","Voting","Reproductive Justice","Religion","Domestic Partner Benefits","Safety")  # Replace tag1, tag2, tag3 with the tags you want to remove

# Filter out the tags to remove from the combined data frame
filtered_combined_tag_counts <- combined_tag_counts_percentages_Is %>%
  filter(!tag %in% tags_to_remove) %>% print (n = 21)

custom_palette <- c("black", "grey")  # Adjusted to have grey below bars

# Plot the data using ggplot2
c <- ggplot(filtered_combined_tag_counts, aes(x = tag, y = Percentage, fill = Group)) +
  geom_bar(stat = "identity", position = position_dodge(width = 0.9), width = 0.6) +
  geom_text(aes(label = ifelse(Count > 0, paste("N =", Count), "")), 
            position = position_dodge(width = 0.9), 
            vjust = -0.5,  # Adjust vertical position
            size = 2.5) +
  geom_hline(yintercept = 0, color = "black", size = 0.5) +  # Add axis line at y = 0
  labs(title = "Figure 3: Agenda setting between AFL-CIO and PAW",
       x = "",
       y = "Percentage") +
  scale_fill_manual(values = custom_palette) +  # Applying custom color palette
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 9),
        legend.position = "bottom",
        # Black and white theme modifications
        text = element_text(color = "black"),
        panel.background = element_rect(fill = "white"),  # Changing background to white
        plot.background = element_rect(fill = "white"),  # Changing background to white
        legend.background = element_rect(fill = "white"),  # Changing background to white
        panel.grid.major = element_blank(),  # Remove major grid lines
        panel.grid.minor = element_blank(),  # Remove minor grid lines
        strip.background = element_rect(fill = "white"),
        strip.text = element_text(color = "black"))
c
ggsave("figure3.tiff", 
       plot = c, width = 10, height = 6, units = "in", dpi = 300)




